from machine import Pin
from neopixel import NeoPixel
import time
import random


class RGB:
    def __init__(self, pin, num) -> None:
        self.rgb = NeoPixel(Pin(pin, Pin.OUT), num)  # RGB灯初始化
        self.rgb_flag = 0  # RGB灯标志位
        self.num = num  # RGB灯数量
        self.colors = {
            "red": (255, 0, 0),  # 红色 0
            "orange": (255, 127, 0),  # 橙色 1
            "yellow": (255, 255, 0),  # 黄色 2
            "green": (0, 255, 0),  # 绿色 3
            "blue": (0, 0, 255),  # 蓝色 4
            "indigo": (75, 0, 130),  # 靛蓝色 5
            "purple": (148, 0, 211),  # 紫色 6
            "colourless": (0, 0, 0),  # 无光 7
        }  # 灯的颜色字典
        self.colors_number = [
            (255, 0, 0),  # 红色 0
            (255, 127, 0),  # 橙色 1
            (255, 255, 0),  # 黄色 2
            (0, 255, 0),  # 绿色 3
            (0, 0, 255),  # 蓝色 4
            (75, 0, 130),  # 靛蓝色 5
            (148, 0, 211),  # 紫色 6
            (0, 0, 0),  # 无光 7
        ]  # 灯的颜色列表
        self.light = 200  # 灯亮度
        self.last_light = self.light  # 灯上一次的亮度
        self.R = 255  # RGB的红色值
        self.G = 255  # RGB的绿色值
        self.B = 255  # RGB的蓝色值
        self.light_speed = 100  # RGB灯效的速度
        self.rgb_count = 0  # RGB 计数器 累计
        self.rgb_number = 0  # RGB 计数器 序号
        self.light_mode = {
            "stop": self.Custom_Lighting_Effects,
            "rgb": self.Colorful_Flowing_Light,
            "light": self.Flashing_Light,
            "write": self.Flowing_Colors,
            "gradient": self.Rainbow_Gradient,
            "breath": self.Breathing_Light,
            "rotate": self.Light_Rotate,
        }  # RGB灯效字典
        self.mode = "gradient"  # RGB灯效
        self.last_mode = self.mode  # RGB上一次灯效

# ============================================================================== #

    '''
    RGB数值写入函数
    '''

    def RGB_Write_Data(self):
        L = float(int(self.light) / 255)
        for index in range(0, self.num):
            self.rgb[index] = (
                int(self.R * L), int(self.G * L), int(self.B * L))
        self.rgb.write()

    def rgb_thread(self):
        while True:
            try:
                self.light_mode.get(self.mode, self.Custom_Lighting_Effects)()
                time.sleep_ms(self.light_speed)
            except Exception as e:
                print("rgb_thread", e)

# ============================================================================== #

    '''
    RGB自定义模式
    '''

    def Custom_Lighting_Effects(self):  # 自定义灯光模式
        self.RGB_Write_Data()

# ============================================================================== #

    '''
    RGB炫彩流光模式
    '''

    def Colorful_Flowing_Light(self):
        self.R, self.G, self.B = self.colors_number[self.rgb_number]  # 获取当前颜色值
        self.RGB_Write_Data()  # 将颜色值写入设备
        self.rgb_number = (self.rgb_number +
                           1) % len(self.colors_number)  # 更新颜色索引
        self.rgb_count += 1  # 增加计数器
        if self.rgb_count >= self.num:  # 如果计数器达到阈值
            self.rgb_count = 0  # 重置计数器

# ============================================================================== #

    '''
    RGB闪光模式
    '''

    def Flashing_Light(self):  # 闪烁灯（不断关灯）
        for index in range(0, self.num):
            self.rgb[index] = self.colors["colourless"]  # 将所有灯光设置为无光
        self.rgb.write()
        time.sleep_ms(self.light_speed)  # 延迟一段时间
        self.RGB_Write_Data()
        time.sleep_ms(self.light_speed)

# ============================================================================== #

    '''
    RGB流动色彩模式
    '''

    def Flowing_Colors(self):
        self.rgb[random.randint(0, self.num - 1)
                 ] = random.choice(self.colors_number) if self.light != 0 else self.colors["colourless"]
        self.rgb.write()
        time.sleep_ms(self.light_speed)

# ============================================================================== #

    '''
    RGB渐变模式
    '''

    def Rainbow_Gradient(self):
        self.rgb[self.rgb_count] = self.colors_number[self.rgb_number] if self.light != 0 else self.colors["colourless"]
        self.rgb_count = (self.rgb_count + 1) % self.num
        self.rgb_number = (self.rgb_number + 1) % 7
        self.rgb.write()

# ============================================================================== #

    '''
    RGB呼吸灯模式
    '''

    def Breathing_Light(self):  # 彩虹呼吸模式
        if self.light < 10:
            self.rgb_flag = 0
        elif self.light > 245:
            self.rgb_flag = 1

        self.light += 5 if self.rgb_flag == 0 else -5

        self.RGB_Write_Data()

# ============================================================================== #

    '''
    RGB两极反转模式
    '''

    def Light_Rotate(self):  # 彩虹旋转模式
        for index in range(0, self.num):
            if index < self.num // 2:
                self.rgb[index] = self.colors["colourless"]  # 关闭前一半的灯光
            else:
                self.rgb[index] = (
                    self.R, self.G, self.B) if self.light != 0 else self.colors["colourless"]  # 打开后一半的灯光
        self.rgb.write()
        time.sleep_ms(self.light_speed * 5)  # 延迟一段时间
        for index in range(0, self.num):
            if index < self.num // 2:
                self.rgb[index] = (
                    self.R, self.G, self.B) if self.light != 0 else self.colors["colourless"]  # 打开前一半的灯光
            else:
                self.rgb[index] = self.colors["colourless"]  # 关闭后一半的灯光
        self.rgb.write()
